package com.tang.starter.config;

import com.tang.common.properties.sys.CoolTeaProperties;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.SpecVersion;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import jakarta.annotation.Resource;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author tang jun liang
 * @Date 2023/8/6 16:40
 */
@SpringBootConfiguration
public class OpenApiConfig {


    @Resource
    private CoolTeaProperties coolTeaProperties;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(info())
                .externalDocs(externalDocs())
                .components(components())
                .specVersion(SpecVersion.V30)
                .addSecurityItem(securityRequirement());
    }

    private Info info() {
        return new Info()
                .title("cool tea")
                .version(coolTeaProperties.getVersion())
                .description("接口文档")
                .license(new License()
                        .name("Apache 2.0") // The Apache License, Version 2.0
                        .url("https://www.apache.org/licenses/LICENSE-2.0.html"))
                .contact(new Contact()
                        .name(coolTeaProperties.getAuthor())
                        .url("https://gitee.com/cool_life")
                        .email("2499185193@qq.com"))
                ;
    }

    private ExternalDocumentation externalDocs() {
        return new ExternalDocumentation()
                .description("cool framework")
                .url("https://gitee.com/cool_life/socializing");
    }

    private Components components() {
        return new Components()
                .addSecuritySchemes("Bearer Authorization",
                        new SecurityScheme()
                                .name("Bearer 认证")
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")
                                .in(SecurityScheme.In.HEADER)
                )
                ;

    }

    private SecurityRequirement securityRequirement() {
        return new SecurityRequirement()
                .addList("Bearer Authorization");
    }

    private List<SecurityRequirement> security(Components components) {
        return components.getSecuritySchemes()
                .keySet()
                .stream()
                .map(k -> new SecurityRequirement().addList(k))
                .collect(Collectors.toList());
    }


    /**
     * 通用接口
     *
     * @return
     */
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("公共接口")
                .packagesToScan("com.tang.portal.controller")
                .build();
    }

    /**
     * 系统接口
     *
     * @return
     */
    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("系统接口")
                .packagesToScan("com.tang.portal.controller.admin")
                .build();
    }

    /**
     * 回调接口
     *
     * @return
     */
    @Bean
    public GroupedOpenApi callbackApi() {
        return GroupedOpenApi.builder()
                .group("回调接口")
                .packagesToScan("com.tang.portal.controller.callback")
                .build();
    }

    /**
     * 流程接口
     *
     * @return
     */
    @Bean
    public GroupedOpenApi flowableApi() {
        return GroupedOpenApi.builder()
                .group("流程接口")
                .packagesToScan("com.tang.portal.controller.flowable")
                .build();
    }

}
